package com.thread.Demo03;

//测试线程的优先级
//默认优先级:5
//设置优先级后,并不是说高的一定先执行,只是有比较大的概率先执行(主要看CPU如何调度)
//如果某段代码比较重要,可以考虑设置高的优先级
public class TestPriority {
    public static void main(String[] args) {
        //主线程的优先级
        System.out.println(Thread.currentThread().getName()+"的优先级-->"+Thread.currentThread().getPriority());
        MyPriority myPriority = new MyPriority();

        Thread t1 = new Thread(myPriority);
        Thread t2 = new Thread(myPriority);
        Thread t3 = new Thread(myPriority);
        Thread t4 = new Thread(myPriority);
        Thread t5 = new Thread(myPriority);
        Thread t6 = new Thread(myPriority);

        //设置优先级
        t1.start();

        t2.setPriority(1);
        t2.start();

        t3.setPriority(4);
        t3.start();

        t4.setPriority(Thread.MAX_PRIORITY);
        t4.start();

//        报错1-10
//        t5.setPriority(-1);
//        t5.start();
//
//        t6.setPriority(11);
//        t6.start();

    }
}


//创建线程类
class MyPriority implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"的优先级-->"+Thread.currentThread().getPriority());
    }
}